package com.sy.service.impl;

import com.github.pagehelper.PageHelper;
import com.sy.entity.Homework;
import com.sy.entity.Teacher;
import com.sy.exception.BusinessException;
import com.sy.mapper.AdminMapper;
import com.sy.mapper.HomeWorkMapper;
import com.sy.mapper.TeacherMapper;
import com.sy.service.TeacherService;
import com.sy.vo.TeacherPagingVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.sy.constant.BusinessConstant.TEACHER_ID;

/**
 * @Author:范富强
 * @Date: 2021/10/11 0011 19:02
 */

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private HomeWorkMapper homeWorkMapper;

    @Autowired
    private TeacherMapper teacherMapper;


    @Override
    public Teacher getTeacherByTno(Integer tno) {
        if (Objects.isNull(tno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "教师编号不能为空");
        }
        Teacher teacherByTno = teacherMapper.getTeacherByTno(tno);
        if (Objects.isNull(teacherByTno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "教师信息未找到");
        }
        teacherByTno.setPwd(null);
        return teacherByTno;
    }

    @Override
    public Integer getTeachersNum() {
        List<Teacher> allTeacher = teacherMapper.getAllTeacher();
        if (allTeacher.isEmpty()) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "教师数量获取失败");
        }
        return allTeacher.size();
    }

    @Override
    public List<Teacher> getAllTeachers(TeacherPagingVo teacherPagingVo) {
        PageHelper.startPage(teacherPagingVo.getPageNum(), teacherPagingVo.getPageSize());
        List<Teacher> allTeacher = teacherMapper.getAllTeacher();
        return allTeacher;
    }

    @Override
    public List<Teacher> getAllTeachersByTeacherNameOrTno(TeacherPagingVo teacherPagingVo) {
        PageHelper.startPage(teacherPagingVo.getPageNum(), teacherPagingVo.getPageSize());
        List<Teacher> teachersByTeacherNameOrTno = teacherMapper.getAllTeachersByTeacherNameOrTno(teacherPagingVo.getTeacherNameOrTno());
        if (teachersByTeacherNameOrTno.isEmpty()){
            throw new BusinessException(HttpStatus.NOT_FOUND, "你输入的教师信息不存在");
        }
        return teachersByTeacherNameOrTno;
    }

    @Override
    public Integer getTeacherNumByTeacherNameOrTno(String teacherNameOrTno) {
        List<Teacher> allTeachersByTeacherNameOrTno = teacherMapper.getAllTeachersByTeacherNameOrTno( teacherNameOrTno);
        if (allTeachersByTeacherNameOrTno.isEmpty()){
            throw new BusinessException(HttpStatus.NOT_FOUND, "你输入的教师信息不存在");
        }
        return allTeachersByTeacherNameOrTno.size();
    }

    @Override
    public Boolean delTeacherById(Integer id) {
        Boolean aBoolean = teacherMapper.delTeacherById(id);
        if (!aBoolean){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "删除教师信息失败");
        }
        return aBoolean;
    }

    @Override
    public Boolean redactTeacher(Teacher teacher) {
        Boolean aBoolean = teacherMapper.redactTeacher(teacher);
        if (!aBoolean){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "删除教师信息失败");
        }
        return aBoolean;
    }

    @Override
    public Boolean addTeacher(Teacher teacher) {
        Boolean aBoolean = teacherMapper.addTeacher(teacher);
        if (!aBoolean){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "添加教师信息失败");
        }
        Boolean aBoolean1 = teacherMapper.addTeacherNum(teacher.getId(), teacher.getId() + TEACHER_ID);
        if (!aBoolean1){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "添加教师工号失败");
        }
        return aBoolean &&aBoolean1;
    }

    @Override
    public List<Integer> getTeacherByLikeName(String teacherName) {
        if (StringUtils.isBlank(teacherName)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "教师的姓名不能为空");
        }
        List<Integer> tnos=new ArrayList<>();
        List<Teacher> teachers = teacherMapper.getTeacherByLikeName(teacherName);
        if (Objects.isNull(teachers)){
            throw new BusinessException(HttpStatus.NOT_FOUND, "查询的教师名未存在...");
        }
        for (Teacher teacher : teachers) {
            tnos.add(teacher.getTno());
        }
        return tnos;
    }

    @Override
    public List<Teacher> getAllTeacherInfo() {
        return  teacherMapper.getAllTeacher();
    }
}
